var app = getApp();
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inTheaters: {},
    comingSoon: {},
    top50: {},
    searchResult: {},
    containerShow: true,
    searchShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    wx.showLoading({
      title: '加载中',
    })
    var doubanurl = app.globalData.doubanurl;
    var inTheaters = "/v2/movie/in_theaters?count=3";
    var comingSoon = "/v2/movie/coming_soon?count=3";
    var top50 = "/v2/movie/top250?count=3";

    this.getMovieListData(doubanurl + inTheaters, 'inTheaters','正在热映');
    this.getMovieListData(doubanurl + comingSoon, 'comingSoon','即将上映');
    this.getMovieListData(doubanurl + top50, 'top50','top250');
    wx.hideLoading()

   // console.log(util.convertToStarsArray(35));
  },

  getMovieListData: function(url, key, cotegoryTitle) {
    var that = this;
    wx.request({
      url: url,
      method: 'GET',
      data: {},
      header: {
        "Content-Type": "application/xml"
      },
      success: function(res) {
        that.porcessDoubanData(res.data, key, cotegoryTitle)
      },
      fail: function(error) {
        console.log(error);
      }
    })
  },
  porcessDoubanData: function (doubanData, key, cotegoryTitle) {
    var movies = [];
    for (var ids in doubanData.subjects) {
      var subject = doubanData.subjects[ids];
      var title = subject.title;
      if (title.length >= 6) {
        title = title.substring(0, 6) + '...';
      }
      var temp = {
        title: title,
        average: subject.rating.average,
        stars: util.convertToStarsArray(subject.rating.stars),
        coverageUrl: subject.images.large,
        movieId: subject.id
      }
      movies.push(temp);
    }

    var readyData = {};
    readyData[key] = {
      cotegoryTitle: cotegoryTitle,
      movies: movies
    };
    this.setData(readyData);
   // console.log(movies);
  },

  onMoveTap: function(event){  
    var cetegory = event.currentTarget.dataset.cetegory;
    wx.navigateTo({
      url: 'move-movie/move-movie?cetegory=' + cetegory,
    })
  },
  onMovieTap: function (event){
 
    var movieId = event.currentTarget.dataset.movieid;
    console.log(movieId);
    wx.navigateTo({
      url: 'movie-detail/movie-detail?movieId=' + movieId,
    })
  },

  onBindfocus: function(event){
    console.log('onBindfocus');
    this.setData({
      containerShow:false,
      searchShow: true
    })
  },
  onCancelImgTap: function(event){
    this.setData({
      containerShow: true,
      searchShow: false,
      searchResult: {}
    })
  },
  onBindinput: function(event){
    var value = event.detail.value
    var searchUrl = app.globalData.doubanurl + '/v2/movie/search?q='+value;
    this.getMovieListData(searchUrl,"searchResult","")
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})